<template>
  <div class="app-container">
    <!--  搜索  -->
    <search-form
      v-show="showSearch"
      :data="queryParams"
      :field-list="filterInfo.fieldList"
      :list-type-info="filterInfo.dictList"
      @handleSearch="handleQuery"
      @handleReset="resetQuery" />

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:config:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:config:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:config:remove']"
          >删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:config:export']"
        >导出
        </el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="handleRefreshCache" v-hasPermi="['system:config:remove']"
          >刷新缓存
        </el-button>
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>
    <hk-table
      :loading="loading"
      :tableData="configList"
      :tableColumns="tableFieldList"
      :total="total"
      :selection="true"
      :showPag="true"
      :operate="true"
      :queryParams="queryParams"
      @changePage="changePage"
      @handleSelectionChange="handleSelectionChange">
      <!--    系统内置    -->
      <template #configType="scope">
        <span class="Inspan" :class="scope.row.configType == 'Y' ? 'simple' : 'serious'">{{
          scope.row.configType == 'Y' ? '是' : '否'
        }}</span>
      </template>
      <!--    操作按钮    -->
      <template #operate="scope">
        <el-button size="mini" type="text" @click="handleUpdate(scope.row)" v-hasPermi="['system:config:edit']"
          >修改
        </el-button>
        <el-button size="mini" type="text" @click="handleDelete(scope.row)" v-hasPermi="['system:config:remove']"
          >删除
        </el-button>
      </template>
    </hk-table>

    <!-- 添加或修改参数配置对话框 -->
    <edit-dialog
      ref="editDialogRef"
      :title="title"
      :form-data="form"
      :config-list="filterInfo.dictList.configList"
      :config-status="filterInfo.dictList.configList"
      @submitForm="submitForm" />
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, refreshCache } from '@/api/system/config'
import SearchForm from '@/components/HKSearchform/index.vue'
import HkTable from '@/components/HKTable'
import editDialog from './form'

export default {
  name: 'Config',
  components: { SearchForm, HkTable, editDialog },
  data() {
    return {
      // 搜索框
      filterInfo: {
        fieldList: [
          { label: '参数名称', type: 'input', field: 'configName' },
          { label: '参数键名', type: 'input', field: 'configKey' },
          { label: '系统内置', type: 'select', field: 'configType', list: 'configList' },
        ],
        dictList: {
          configList: [
            { label: '是', value: 'Y' },
            { label: '否', value: 'N' },
          ],
        },
      },
      // 表格配置项
      tableFieldList: [
        { label: '参数编号', param: 'id' },
        { label: '参数名称', param: 'configName' },
        { label: '参数键名', param: 'configKey' },
        { label: '参数键值', param: 'configValue' },
        { label: '系统内置', param: 'configType', type: 'slot' },
        { label: '备注', param: 'remark',attrs:{maxlength:200} },
        { label: '创建时间', param: 'createDate' },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        configName: undefined,
        configKey: undefined,
        configType: undefined,
      },
      // 表单参数
      form: {},
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 分页 */
    changePage(data) {
      this.queryParams.current = data.current
      this.queryParams.size = data.size
      this.getList()
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true
      listConfig(this.queryParams).then((response) => {
        this.configList = response.list
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    submitForm() {
      this.getList()
    },

    /** 搜索按钮操作 */
    handleQuery(data) {
      if (data) {
        this.queryParams.configName = data.configName || undefined
        this.queryParams.configKey = data.configKey || undefined
        this.queryParams.configType = data.configType || undefined
      } else {
        this.queryParams = {
          current: 1,
          size: 10,
        }
      }
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {
        configName: undefined,
        configKey: undefined,
        configValue: undefined,
        configType: 'Y',
        remark: undefined,
      }
      this.$refs.editDialogRef.show()
      this.title = '添加参数'
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const configId = row.id || this.ids
      getConfig(configId).then((response) => {
        this.form = response
        this.$refs.editDialogRef.show()
        this.title = '修改参数'
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.id || this.ids
      this.$modal
        .confirm('是否确认删除参数编号为"' + configIds + '"的数据项？')
        .then(function () {
          return delConfig(configIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/config/export',
        {
          ...this.queryParams,
        },
        `config_${new Date().getTime()}.xlsx`
      )
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess('刷新成功')
      })
    },
  },
}
</script>
